home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH1 / SRC / RESIZE.FRM < prev    next >
Text File  |  1996-01-04  |  5KB  |  180 lines

  1. VERSION 4.00
  2. Begin VB.Form ResizeForm 
  3.    Caption         =   "Resize"
  4.    ClientHeight    =   4140
  5.    ClientLeft      =   1500
  6.    ClientTop       =   1725
  7.    ClientWidth     =   6690
  8.    Height          =   4830
  9.    Left            =   1440
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4140
  12.    ScaleWidth      =   6690
  13.    Top             =   1095
  14.    Width           =   6810
  15.    Begin VB.Label RedrawLabel 
  16.       BorderStyle     =   1  'Fixed Single
  17.       Height          =   255
  18.       Left            =   960
  19.       TabIndex        =   1
  20.       Top             =   285
  21.       Width           =   855
  22.    End
  23.    Begin VB.Label RecalcLabel 
  24.       BorderStyle     =   1  'Fixed Single
  25.       Height          =   255
  26.       Left            =   960
  27.       TabIndex        =   0
  28.       Top             =   0
  29.       Width           =   855
  30.    End
  31.    Begin VB.Label Label1 
  32.       Caption         =   "Calculations"
  33.       Height          =   255
  34.       Index           =   0
  35.       Left            =   0
  36.       TabIndex        =   2
  37.       Top             =   0
  38.       Width           =   855
  39.    End
  40.    Begin VB.Label Label1 
  41.       Caption         =   "Draws"
  42.       Height          =   255
  43.       Index           =   1
  44.       Left            =   0
  45.       TabIndex        =   3
  46.       Top             =   285
  47.       Width           =   495
  48.    End
  49.    Begin VB.Menu mnuFile 
  50.       Caption         =   "&File"
  51.       Begin VB.Menu mnuFileExit 
  52.          Caption         =   "E&xit"
  53.       End
  54.    End
  55. End
  56. Attribute VB_Name = "ResizeForm"
  57. Attribute VB_Creatable = False
  58. Attribute VB_Exposed = False
  59. Option Explicit
  60.  
  61. ' Counts for recalculations and redraws.
  62. Dim recalcs As Integer
  63. Dim redraws As Integer
  64.  
  65. ' Drawing parameters.
  66. Dim x1 As Single    ' The center of the face.
  67. Dim y1 As Single
  68. Dim x2 As Single    ' The left eye's center.
  69. Dim y2 As Single
  70. Dim x3 As Single    ' The right eye's center.
  71. Dim y3 As Single
  72. Dim x4 As Single    ' The right pupil's center.
  73. Dim y4 As Single
  74. Dim x5 As Single    ' The right pupil's center.
  75. Dim y5 As Single
  76. Dim r1 As Single    ' The face's radius.
  77. Dim r2 As Single    ' The mouth's radius.
  78. Dim r3 As Single    ' The eyes' radii.
  79. Dim r4 As Single    ' The pupils' radii.
  80.  
  81. ' ***********************************************
  82. ' Recalculate drawing parameters. Make the face
  83. ' as large as possible while keeping a 10% margin
  84. ' around all edges.
  85. ' ***********************************************
  86. Sub CalculateParameters()
  87. Dim r4_2 As Single
  88.  
  89.     x1 = ScaleWidth / 2
  90.     y1 = ScaleHeight / 2
  91.     If x1 < y1 Then
  92.         r1 = x1 * 0.9
  93.     Else
  94.         r1 = y1 * 0.9
  95.     End If
  96.     r2 = r1 * 0.7
  97.     r3 = r1 * 0.2
  98.     r4 = r3 * 0.5
  99.     
  100.     x2 = x1 - r2 * 0.7
  101.     y2 = y1 - r2 * 0.5
  102.     x3 = x1 + r2 * 0.7
  103.     y3 = y2
  104.     
  105.     r4_2 = r4 / Sqr(2)
  106.     x4 = x2 + r4_2
  107.     y4 = y2 + r4_2
  108.     x5 = x3 + r4_2
  109.     y5 = y3 + r4_2
  110.  
  111.     ' Update the recalc count.
  112.     recalcs = recalcs + 1
  113.     RecalcLabel.Caption = Format$(recalcs)
  114. End Sub
  115.  
  116. ' ***********************************************
  117. ' Draw stuff on the form.
  118. ' ***********************************************
  119. Sub DrawForm()
  120. Const PI = 3.14159
  121. Dim old_fill As Integer
  122.  
  123.     Cls                         ' Erase the form.
  124.     Circle (x1, y1), r1         ' Face
  125.     Circle (x1, y1), r2, , PI + 0.3, 2 * PI - 0.3 ' Smile
  126.     Circle (x1, y1), r3         ' Nose
  127.     Circle (x2, y2), r3         ' Left eye.
  128.     Circle (x3, y3), r3         ' Right eye.
  129.     
  130.     old_fill = FillStyle
  131.     FillStyle = vbSolid
  132.     Circle (x4, y4), r4         ' Left pupil.
  133.     Circle (x5, y5), r4         ' Right pupil.
  134.     FillStyle = old_fill
  135.  
  136.     ' Update the redraw count.
  137.     redraws = redraws + 1
  138.     RedrawLabel.Caption = Format$(redraws)
  139. End Sub
  140. ' ***********************************************
  141. ' Redraw the form.
  142. ' ***********************************************
  143. Private Sub Form_Paint()
  144.     DrawForm
  145. End Sub
  146. ' ***********************************************
  147. ' If we are minimized, do nothing. Otherwise
  148. ' recompute the drawing parameters. Then if
  149. ' neither the height nor the width have
  150. ' increased, we are shrinking so redraw the form.
  151. ' ***********************************************
  152. Private Sub Form_Resize()
  153. Static wid As Single
  154. Static hgt As Single
  155.  
  156.     ' If we're minimized, do nothing.
  157.     If WindowState = vbMinimized Then Exit Sub
  158.     
  159.     ' If the size has changed, recalculate the
  160.     ' drawing parameters. This will not happen if
  161.     ' we are being restored after being minimized.
  162.     If Width <> wid Or Height <> hgt Then _
  163.         CalculateParameters
  164.     
  165.     ' If we are shrinking, redraw now.
  166.     If Not (Width >= wid And Height >= hgt) Then _
  167.         DrawForm
  168.         
  169.     ' Save the new width and height.
  170.     wid = Width
  171.     hgt = Height
  172. End Sub
  173.  
  174.  
  175. Private Sub mnuFileExit_Click()
  176.     Unload Me
  177. End Sub
  178.  
  179.  
  180.